package com.j180.config;


import com.j180.intercepter.LoginInterceputer;
import com.j180.intercepter.OneInterceper;
import com.j180.conversion.StringToLocalDate;
import com.j180.intercepter.TwoInteceputer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.j180.*")
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public InternalResourceViewResolver getViewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/pages/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    //配置 静态资源处理器 <mvc:default-servlet-handler/>
    //对应静态支援都全部放行
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //注册一个装换器
    @Override
    public void addFormatters(FormatterRegistry registry) {

        registry.addConverter(new StringToLocalDate());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

         //配置拦截器
        // 拦截 /inter/* 所有的方法 排除 /inter/two
        //拦截器的执行顺序为 注解的顺序
       // registry.addInterceptor(new TwoInteceputer()).addPathPatterns("/inter/*").excludePathPatterns("/inter/two");

      //  registry.addInterceptor(new OneInterceper()).addPathPatterns("/inter/*").excludePathPatterns("/inter/two");

       // registry.addInterceptor(new LoginInterceputer()).excludePathPatterns("/user/login","login.jsp");
    }
}
